﻿using CatUtils.Utils.Currency.IoUtils;
using Service_monitoring.Utils.MailHelper;
using Service_monitoring.Utils.MailHelper.MailHelper;
using Service_monitoring.Utils.UserUtils;
using Service_monitoring.Utils.ViewHleper;
using Service_monitoring.Utils.WindowsHelper;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace Service_monitoring
{
    public partial class MainView : Form
    {
        public MainView()
        {
            InitializeComponent();
            InitService();
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Application.Restart();
        }

        private void InitService()
        {
            SendMessageHelper.Send("SystemStart");
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().InitCheckboxSaveInfo(CbCloseScreen);
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().InitCheckboxSaveInfo(CbHideBar);
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().InitCheckboxSaveInfo(CbHideConsole);
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().InitCheckboxSaveInfo(CbProgremBar);
            UserStaticValue.CloseScreen = CbCloseScreen.Checked;
            UserStaticValue.HideBar = CbHideBar.Checked;
            UserStaticValue.HideConsole = CbHideConsole.Checked;
            UserStaticValue.HideProgremBar = CbProgremBar.Checked;
        }

        private int keycount = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            AutoSetting(CbCloseScreen.Checked, CbHideConsole.Checked, CbHideBar.Checked);
            return;
            SendMessageHelper.Send("SendMessageHelper");
            ViewOperate.HideConsole();
            ViewOperate.HideTaskbar();
            //TelegramInformation.SendMessage("hellow", 701156221);
            //HideView(this);
        }

        private void MainView_KeyDown(object sender, KeyEventArgs e)
        {
            SendMessageHelper.Send(e.KeyValue.ToString());

            if (e.KeyValue == 48)
            {
                SendMessageHelper.Send(keycount++.ToString());

                if (keycount != 0 && keycount % 5 == 0)
                {
                    AutoSetting(false, false, false);
                }
            }
        }

        /// <summary>
        /// 根据系统配置关闭打开屏幕
        /// </summary>
        /// <param name="CloseScreenTemp">true关闭，flase打开</param>
        /// <param name="HideConsoleTemp">true关闭，flase打开</param>
        /// <param name="HideBarTemp">true关闭，flase打开</param>
        public void AutoSetting(bool CloseScreenTemp, bool HideConsoleTemp, bool HideBarTemp)
        {
            SendMessageHelper.Send($"Show View CloseScreenTemp{CloseScreenTemp} HideConsoleTemp{HideConsoleTemp}HideBarTemp{HideBarTemp}");
            ///显示器
            if (CloseScreenTemp)
            {
                TopMost = true;
                WindowState = FormWindowState.Maximized;
                ViewOperate.SendMessage(Handle, ViewOperate.WM_SYSCOMMAND, ViewOperate.SC_MONITORPOWER, 2); //关闭显示器;
            }
            else
            {
                ViewOperate.SendMessage(Handle, ViewOperate.WM_SYSCOMMAND, ViewOperate.SC_MONITORPOWER, -1); //打开显示器;
                FormHelper.ShowView(this, 500, 500, FormBorderStyle.FixedDialog, true);
            }
            //程序控制台
            if (HideConsoleTemp)
                ViewOperate.HideConsole();
            else
                ViewOperate.ShowConsole();
            //bar
            if (HideBarTemp)
                ViewOperate.HideTaskbar();
            else
                ViewOperate.ShowTaskbar();
        }

        private void MainView_ResizeEnd(object sender, EventArgs e)
        {
            SendMessageHelper.Send("Move Pragram OR Resize");
            FormHelper.HideView(this, 1, 1, FormBorderStyle.Fixed3D, false);
            // Hide();
        }

        private void MainView_Click(object sender, EventArgs e)
        {
            SendMessageHelper.Send("click");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AutoSetting(false, false, false);
        }

        private void MainView_FormClosing(object sender, FormClosingEventArgs e)
        {
            SendMessageHelper.Send("MainView_FormClosing");
            Console.WriteLine("关闭");
        }

        private void MainView_FormClosed(object sender, FormClosedEventArgs e)
        {
            SendMessageHelper.Send("MainView_FormClosed");
        }

        private void CbCloseScreen_CheckedChanged(object sender, EventArgs e)
        {
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().SaveCheckboxInfo(CbCloseScreen);
        }

        private void CbHideBar_CheckedChanged(object sender, EventArgs e)
        {
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().SaveCheckboxInfo(CbHideBar);
        }

        private void CbHideConsole_CheckedChanged(object sender, EventArgs e)
        {
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().SaveCheckboxInfo(CbHideConsole);
        }

        private void CbProgremBar_CheckedChanged(object sender, EventArgs e)
        {
            new CatUtils.Utils.SpecialUtils.View.ViewInfoMemory().SaveCheckboxInfo(CbProgremBar);
        }
    }
}